// The NSpring Framework for .NET
// Copyright (c) 2003, Jeffrey Varszegi
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
// 
// - Redistributions of source code must retain the above copyright notice, this 
// list of conditions and the following disclaimer.
// 
// - Redistributions in binary form must reproduce the above copyright notice, this 
// list of conditions and the following disclaimer in the documentation and/or other 
// materials provided with the distribution.
// 
// - Neither the name of the NSpring project nor the names of its contributors may 
// be used to endorse or promote products derived from this software without 
// specific prior written permission from the copyright owner.
// 
// - No product derived from this software may be called "NSpring", nor may 
// "NSpring" appear in the name of such a product, without specific prior written 
// permission from the copyright owner.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE. 

namespace NSpring.Logging.Common.Pattern {

using System;
using System.Text;

using NSpring.Logging;

/// <summary>
/// Types of valid values for context-property tokens.
/// </summary>
internal enum ContextPropertyType {
    ApplicationName,
    SystemName,
    UserName,
    ThreadName,
    ThreadPriority,
    Property
}

/// <summary>
/// <para>
/// Each ContextPropertyToken is a pattern placeholder for a property of an Event object's Context property
/// All underlying pattern-related code is subject to change without warning
/// or deprecation, and should not be called directly by application code
/// </para>
/// </summary>
internal sealed class ContextPropertyToken : Token {
    private const string BLANK_STRING                   = "";
    private const char SPACE                            = ' ';
    
    private Alignment alignment;
    private Case __case;
    private int minimumWidth;
    private int maximumWidth;
    
    private ContextPropertyType propertyType;
    private string propertyName;
    
    private ContextPropertyToken() {}
    
    public ContextPropertyToken(ContextPropertyType _propertyType, string _propertyName, string _text, int _minimumWidth, int _maximumWidth, Alignment _alignment, Case _case) {
        type = TokenType.ContextProperty;
        propertyType = _propertyType;
        propertyName = _propertyName;
        text = _text;
        minimumWidth = Math.Max(_minimumWidth, 0);
        maximumWidth = Math.Max(minimumWidth, _maximumWidth);
        alignment = _alignment;
        __case = _case;
    }
    
    public ContextPropertyType PropertyType {
        get {
            return propertyType;
        }
    }
    
    public string PropertyName {
        get {
            return propertyName;
        }
    }

    public Alignment Alignment {
        get {
            return alignment;
        }
    }
    
    public Case Case {
        get {
            return __case;
        }
    }

    public int MinimumWidth {
        get {
            return minimumWidth;
        }
    }

    public int MaximumWidth {
        get {
            return maximumWidth;
        }
    }
    
    public override string Format(Event _event) {
        Context context = _event.Context;
        string s = null;
        if (context != null) {
            switch (propertyType) {
                case ContextPropertyType.ApplicationName           : {
                    s = context.ApplicationName;
                    break;
                }
                case ContextPropertyType.SystemName                : {
                    s = context.SystemName;
                    break;
                }
                case ContextPropertyType.UserName                  : {
                    s = context.UserName;
                    break;
                }
                case ContextPropertyType.ThreadName                : {
                    s = context.ThreadName;
                    break;
                }
                case ContextPropertyType.ThreadPriority            : {
                    s = context.ThreadPriority.ToString();
                    break;
                }
                case ContextPropertyType.Property                  : {
                    object o = context.GetProperty(propertyName);
                    if (o != null) {
                        s = o.ToString();
                    }
                    break;
                }
            }
        }
        if (s == null) {
            if (minimumWidth == 0) {
                s = BLANK_STRING;
            }
            else {
                s = BLANK_STRING.PadLeft(minimumWidth, SPACE);
            }
        }
        else {
            if (s.Length < minimumWidth) {
                switch (alignment) {
                    case Alignment.Center   : {
                        int leftMargin = ((minimumWidth - s.Length) / 2);
                        if (leftMargin > 0) {
                            s = s.PadLeft((s.Length + leftMargin), SPACE);
                        }
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Left     : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Right    : {
                        s = s.PadLeft(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Default  : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                }
            }
            else if (s.Length > maximumWidth) {
                s = s.Substring(0, maximumWidth);
            }
        }
        switch (__case) {
            case Case.Lower     : {
                s = s.ToLower();
                break;
            }
            case Case.Upper     : {
                s = s.ToUpper();
                break;
            }
            case Case.Default   : {
                break;
            }
        }
        return s;
    }
    
    public override void Format(Event _event, StringBuilder sb) {
        Context context = _event.Context;
        string s = null;
        if (context != null) {
            switch (propertyType) {
                case ContextPropertyType.ApplicationName           : {
                    s = context.ApplicationName;
                    break;
                }
                case ContextPropertyType.SystemName                : {
                    s = context.SystemName;
                    break;
                }
                case ContextPropertyType.UserName                  : {
                    s = context.UserName;
                    break;
                }
                case ContextPropertyType.ThreadName                : {
                    s = context.ThreadName;
                    break;
                }
                case ContextPropertyType.ThreadPriority            : {
                    s = context.ThreadPriority.ToString();
                    break;
                }
                case ContextPropertyType.Property                  : {
                    object o = context.GetProperty(propertyName);
                    if (o != null) {
                        s = o.ToString();
                    }
                    break;
                }
            }
        }
        if (s == null) {
            if (minimumWidth == 0) {
                return;
            }
            else {
                sb.Append(BLANK_STRING.PadLeft(minimumWidth, SPACE));
                return;
            }
        }
        else {
            if (s.Length < minimumWidth) {
                switch (alignment) {
                    case Alignment.Center   : {
                        int leftMargin = ((minimumWidth - s.Length) / 2);
                        if (leftMargin > 0) {
                            s = s.PadLeft((s.Length + leftMargin), SPACE);
                        }
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Left     : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Right    : {
                        s = s.PadLeft(minimumWidth, SPACE);
                        break;
                    }
                    case Alignment.Default  : {
                        s = s.PadRight(minimumWidth, SPACE);
                        break;
                    }
                }
            }
            else if (s.Length > maximumWidth) {
                s = s.Substring(0, maximumWidth);
            }
        }
        switch (__case) {
            case Case.Lower     : {
                sb.Append(s.ToLower());
                return;
            }
            case Case.Upper     : {
                sb.Append(s.ToUpper());
                return;
            }
            case Case.Default   : {
                sb.Append(s);
                return;
            }
        }
    }
}

}
